﻿NAMFox.ns(function() { with (NAMFox) {

name("Message Manager Tests");

//////////////////////////////////////////////
// Helper Methods
//////////////////////////////////////////////

var tmpDir = $("@mozilla.org/file/directory_service;1").service("nsIProperties").get("TmpD", $("nsIFile")).path;
var pathSeparator = tmpDir.search(/\\/) === -1 ? "/" : "\\";
tmpDir += pathSeparator;
var tmpFile = tmpDir + "tmp.ncm";

function assertSnapshotMatchesCurrent(expected) {
    var actual = takeMessageSnapshot();
    
    equals(expected.length, actual.length, "The number of messages in between snapshots must be equal.");
    
    if (expected.length === actual.length) {
        for (var i in expected) {
            equals(expected[i].name, actual[i].name, "The name of message " + i + " should be consistent between snapshots.");
            equals(expected[i].value, actual[i].value, "The value of message " + i + " should be consistent between snapshots.");
        }
    }
}

function takeMessageSnapshot() {
    var snapshot = { length: 0 };
    $.messages.enumerate(function (msg) {
        snapshot[msg.key] = { key: msg.key, name: msg.name, text: msg.text };
        snapshot.length++;
    });
    
    snapshot[$.messages.getDefaultMessage().key] = { name: "Default Message", text: $.messages.getDefaultMessage().text };
    snapshot.length++;
    
    return snapshot;
}

//////////////////////////////////////////////
// Test Cases
//////////////////////////////////////////////

test("Backup can be restored", function() {
    var snapshot = takeMessageSnapshot();
    $.messages.backup(tmpFile);

    // Clear messages
    $.messages.enumerate(function(msg) {
        msg.name = msg.text = new Date().toString();
    });

    $.messages.restore(tmpFile);

    assertSnapshotMatchesCurrent(snapshot);
});

test("Enumeration count matches size", function() {
    var count = 0;
            
    $.messages.enumerate(function() {
        ++count;
    });
    
    equals(count, $.messages.size(), "The number of messages exposed by the size() function should match the number of messages used by the enumerate() function.");
});

test("Enumeration does not stop if callback throws", function() {
    var count = 0;

    $.messages.enumerate(function() {
        ++count;
        throw new Error("Should not stop.");
    });
});

test("Returns default message", function() {
    var defaultMessage = $.messages.getDefaultMessage();

    ok(defaultMessage.QueryInterface(Components.interfaces.nfxICustomMessage), "The default message should be an nfxICustomMessage.");
    
    var name = new Date().toTimeString();
    defaultMessage.text = name;
    defaultMessage = $.messages.getDefaultMessage();
    
    equals(name, defaultMessage.text, "The default message should keep its value.");
});

test("getMessage returns same messages from enumerate", function() {
    // Easy way to look at all messages.
    var snapshot = takeMessageSnapshot();

    for (var i in snapshot) {
        if (snapshot.hasOwnProperty(i) && i !== "length" && i !== "defaultMessage") {
            var expected = snapshot[i];
            var actual = $.messages.getMessage(i);
            equals(expected.key, actual.key, "The keys should match.");
            equals(expected.text, actual.text, "The message values should match.");
            
            if (expected.name) {
                equals(expected.name, actual.name, "The message names should match.");
            }
        }
    }
});

test("restore retrieves new messages from disk", function() {
    var snapshot = takeMessageSnapshot();

    $.messages.backup(tmpFile);

    // Clear messages
    $.messages.enumerate(function(msg) {
        msg.name = msg.text = new Date().toString();
    });

    // Save our changes. This means in order for the test to pass
    // the restore function must also save the restored file to
    // the default file.
    $.messages.save();

    $.messages.restore(tmpFile);
    $.messages.refresh();

    assertSnapshotMatchesCurrent(snapshot);
});

test("restore throws if file not found", function() {
    expectError(function() { $.messages.restore("@*#($"); }, "NS_ERROR_FILE_UNRECOGNIZED_PATH");
});

test("save flushes message values", function() {
    var value = 0;

    $.messages.enumerate(function (msg) {
        msg.text = (++value).toString();
    });
    $.messages.getDefaultMessage().text = "rand";

    var snapshot = takeMessageSnapshot();
    $.messages.save();

    $.messages.enumerate(function (msg) {
        msg.text = "";
    });
    $.messages.getDefaultMessage().text = "";

    $.messages.refresh();

    assertSnapshotMatchesCurrent(snapshot);
});

}});